//
//  DetailsViewController.m
//  项目
//
//  Created by 焦钟培 on 15-1-4.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailsViewController.h"
#import "ReadingRoomModel.h"
#import "ConnectModel.h"
#import "DetailBace.h"
#import "DetailModel.h"
#import <ShareSDK/ShareSDK.h>
#import "DengLuViewController.h"

#define kBlueColor [UIColor colorWithRed:129/255.0 green: 198/255.0 blue: 221/255.0 alpha: 1.0]

@interface DetailsViewController ()

@end

@implementation DetailsViewController
- (void)dealloc
{
    [_modelArray release];
    [_arrays release];
    [_plotArray release];
    [_parameter release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.parameter = [[ReadingRoomModel alloc] init];
        [_parameter release];
        self.plotArray = [NSMutableArray array];
        self.arrays = [NSMutableArray array];
        self.modelArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self start];
    // Do any additional setup after loading the view.
}

#warning 解析
- (void)start
{
    NSDictionary *dic1 = [NSDictionary dictionaryWithObject:_parameter.os forKey:@"os"];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObject:_parameter.ver forKey:@"ver"];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObject:_parameter.op forKey:@"op"];
    NSDictionary *dic4 = [NSDictionary dictionaryWithObject:_parameter.cid forKey:@"cid"];
    NSDictionary *dic5 = [NSDictionary dictionaryWithObject:_parameter.aid forKey:@"aid"];
    NSDictionary *dic6 = [NSDictionary dictionaryWithObject:_parameter.p forKey:@"p"];
    NSDictionary * dic7 = [NSDictionary dictionaryWithObject:_parameter.w forKey:@"w"];
    NSDictionary *dic8 =[NSDictionary dictionaryWithObject:_parameter.cFrom forKey:@"cFrom"];
    NSMutableArray *arrays = [NSMutableArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5,dic6,dic7,dic8, nil];
    [_arrays addObjectsFromArray:arrays];
    
    [ConnectModel connectModelWithUrlStr:@"http://mobi.360doc.com/v17/ajax/article.ashx" parmaters:arrays block:^(NSData *data) {
        
        NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
        NSString *str = [result stringByReplacingOccurrencesOfString:@"({" withString:@"{"];
        NSString *strs = [str stringByReplacingOccurrencesOfString:@"})" withString:@"}"];
        NSData *newData = [strs dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableDictionary * dics = [NSJSONSerialization JSONObjectWithData:newData options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dicFirst = [dics objectForKey:@"aCnt"];
        NSString * path = [dicFirst objectForKey:@"Cnt"];
        NSString *path1 = [path stringByReplacingOccurrencesOfString:@"src=\"imgloading.png\" onclick=\"showBigImage(" withString:@"src="];
        NSDictionary *second = [dics objectForKey:@"aInf"];
        [_modelArray addObject:[second objectForKey:@"SaverUserid"]];
        [_modelArray addObject:[second objectForKey:@"Tit"]];
        
#warning 创建webView
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height - 70)];
        [webView setBackgroundColor:[UIColor whiteColor]];
        
        webView.scrollView.contentSize = CGSizeMake(webView.scrollView.contentSize.width, webView.scrollView.contentSize.height + 80);
        
        webView.scalesPageToFit = NO;
        [webView loadHTMLString:path1 baseURL:nil];
        [self.view addSubview:webView];
        [webView release];
        for (UIView *subView in webView.scrollView.subviews) {
            subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + 80, subView.frame.size.width, subView.frame.size.height);
        }
        
        
#warning Tit
        UILabel *titLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, webView.frame.size.width, 44)];
        titLabel.text = [second objectForKey:@"Tit"];
        titLabel.numberOfLines = 0;
        titLabel.font = [UIFont systemFontOfSize:20];
        titLabel.adjustsFontSizeToFitWidth = YES;
        [webView.scrollView addSubview:titLabel];
        [titLabel release];
        
#warning SUH
        UIImageView *SUHimage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 49, 36, 36)];
        SUHimage.layer.masksToBounds = YES;
        SUHimage.layer.cornerRadius = 18;
        NSURL *SUHurl = [NSURL URLWithString:[second objectForKey:@"SUH"]];
        NSData *SUHdata = [NSData dataWithContentsOfURL:SUHurl];
        SUHimage.image = [UIImage imageWithData:SUHdata];
        [webView.scrollView addSubview:SUHimage];
        [SUHimage release];
        
#warning SNName
        UILabel *SNNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 49, webView.frame.size.width - 50, 18)];
        SNNameLabel.text = [second objectForKey:@"SNName"];
        SNNameLabel.textColor = [UIColor grayColor];
        SNNameLabel.font = [UIFont systemFontOfSize:14];
        [webView.scrollView addSubview:SNNameLabel];
        [SNNameLabel release];
        
#warning SD
        UILabel *SDLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 67, webView.frame.size.width - 50, 18)];
        SDLabel.text = [second objectForKey:@"SD"];
        SDLabel.textColor = [UIColor grayColor];
        SDLabel.font = [UIFont systemFontOfSize:14];
        [webView.scrollView addSubview:SDLabel];
        [SDLabel release];
        
        UIView *views = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
        views.backgroundColor = kBlueColor;
        [self.view addSubview:views];
        
        
        UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(6, 5, self.view.frame.size.width / 5 - 35, 30)];
        [left setBackgroundImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
        [left addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
        [views addSubview:left];
        [left release];
        
        
        UIButton *center = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 5 * 2 - 27, 5, self.view.frame.size.width / 5 - 35, 30)];
        [center setBackgroundImage:[UIImage imageNamed:@"fenxiang.png"] forState:UIControlStateNormal];
        [center addTarget:self action:@selector(centerAction) forControlEvents:UIControlEventTouchUpInside];
        [views addSubview:center];
        [center release];
        
        UIButton *rRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 5 * 3 + 2, 5, self.view.frame.size.width / 5 - 35, 30)];
        [rRight setBackgroundImage:[UIImage imageNamed:@"wenzi.png"] forState:UIControlStateNormal];
        [rRight addTarget:self action:@selector(RrightAction:) forControlEvents:UIControlEventTouchUpInside];
        [views addSubview:rRight];
        [rRight release];
        
        
        self.right = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 5 * 4 + 25, 5, self.view.frame.size.width / 5 - 35, 30)];
        [_right setBackgroundImage:[UIImage imageNamed:@"shoucang1.png"] forState:UIControlStateNormal];
        [_right addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
        [views addSubview:_right];
        [_right release];
        
    }];
}


- (void)leftAction
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

#warning 分享
- (void)centerAction
{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *str = [user objectForKey:@"login"];
    
    if ([str isEqualToString:@"yes"]) {
//        DengLuViewController *dengLuController = [[DengLuViewController alloc] init];
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:dengLuController];
//        [self presentViewController:nav animated:YES completion:^{
//            
//        }];
//        [dengLuController release];
//        [nav release];
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"haiyang" ofType:@"jpg"];
        id<ISSContent> publishContent = [ShareSDK content:@"您想要分享的"
                                           defaultContent:@"测试一下" image:[ShareSDK imageWithPath:imagePath]title:@"ShareSDK"
                                                      url:@"http://www.mob.com" description:@"这是一条测试信息"
                                                mediaType:SSPublishContentMediaTypeNews];
        //创建弹出菜单容器
        
        id<ISSContainer> container = [ShareSDK container];
        
        id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:@"内容分型" oneKeyShareList:[NSArray defaultOneKeyShareList] qqButtonHidden:YES wxSessionButtonHidden:YES wxTimelineButtonHidden:YES showKeyboardOnAppear:YES shareViewDelegate:self friendsViewDelegate:self picViewerViewDelegate:nil];
        
        
        
        //弹出分享菜单
        [ShareSDK showShareActionSheet:container
                             shareList:nil
                               content:publishContent
                         statusBarTips:YES
                           authOptions:nil
                          shareOptions:shareOptions
                                result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                    
                                    if (state == SSResponseStateSuccess)
                                    {
                                        NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                    }
                                    else if (state == SSResponseStateFail)
                                    {
                                        NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                    }
                                }];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"不好意思" message:@"请您先登陆" delegate:self cancelButtonTitle:@"登陆" otherButtonTitles:@"取消", nil];
        
        [alert show];
        [alert release];
    }
    
    
//    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"card"  ofType:@"png"];
//    //构造分享内容
//    id<ISSContent> publishContent = [ShareSDK content:@"分享内容测试"
//                                       defaultContent:@"默认分享内容测试，没内容时显示"
//                                                image:[ShareSDK imageWithPath:imagePath]
//                                                title:@"pmmq"
//                                                  url:@"http://www.sharesdk.cn"
//                                          description:@"这是一条测试信息"
//                                            mediaType:SSPublishContentMediaTypeNews];
//    [ShareSDK showShareActionSheet:nil
//                         shareList:nil
//                           content:publishContent
//                     statusBarTips:YES
//                       authOptions:nil
//                      shareOptions: nil
//                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//                                if (state == SSResponseStateSuccess)
//                                {
//                                    NSLog(@"分享成功");
//                                }
//                                else if (state == SSResponseStateFail)
//                                {
//                                    NSLog(@"分享失败");
//                                }
//                            }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        DengLuViewController *dengController = [[DengLuViewController alloc] init];
        [self presentViewController:dengController animated:YES completion:nil];
        dengController.view.frame = CGRectMake(0, 0, dengController.view.frame.size.width, 0);
        [dengController release];
    }
}


#warning 文字设置点击

- (void)RrightAction:(id)sender
{
    
}

#warning 收藏点击

- (void)rightAction
{
    NSInteger nid = [[_modelArray firstObject] integerValue];
    NSString *str = [ConnectModel connectWithUrlStr:@"http://mobi.360doc.com/v17/ajax/article.ashx" parmaters:_arrays];
    DetailModel *model = [DetailModel detailModelWithDetail_id:nid detail_tit:[_modelArray lastObject] detail_http:str];
   NSString  *show = [DetailBace insertIntoDetailWithDetailModel:model];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:show delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [_right setBackgroundImage:[UIImage imageNamed:@"shoucang2.png"] forState:UIControlStateNormal];
    [alert show];
    [alert release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
